/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Copyright (c) 2011 Michael Chan.
 ******************************************************************************/
package com.google.code.solrdimension.spring;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * This is the configuration containing all User-configurable attributes
 * 
 * @author mhc
 * 
 */
public class ApplicationConfig {

    @Autowired
    @Qualifier("applicationProperties")
    Properties props = null;

   
    
    public String getPropertyAsString(String key, String defaultValue){
        return (props.getProperty(key)!=null?props.getProperty(key):defaultValue);
    }
    
    public boolean getPropertyAsBoolean(String key){
        return Boolean.parseBoolean(props.getProperty(key));
    }
    
    public boolean isApplicationEnabled(){
        return getPropertyAsBoolean(ConfKeyConstants.APPLICATION_ENABLED);
    }
    
    public InputStream getSolrDimensionXmlConfigFileAsInputStream() {

        String solrDimensionXmlConfigFilePath = this.getPropertyAsString(ConfKeyConstants.XML_CONFIG_FILE_PATH_KEY, null);
        
        InputStream stream = null;

        if (solrDimensionXmlConfigFilePath != null && !solrDimensionXmlConfigFilePath.isEmpty()) {

            // first try a class path
            stream = this.getClass().getResourceAsStream(solrDimensionXmlConfigFilePath);
            
            if (stream == null){
                
                //now try file path
                try {
                    stream = new FileInputStream(solrDimensionXmlConfigFilePath);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                
            }

        }
        return stream;
    }

}
